package base;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

import java.io.File;

import utils.ActivityUtil;
import utils.ToastUtils;
import widget.MyDialogView;

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void deleteapk() {
        // 删除已下载的更新文件
        String path = "";
        if (Environment.MEDIA_MOUNTED.equalsIgnoreCase(Environment.getExternalStorageState())) {
            path = Environment.getExternalStorageDirectory() + "/download/iFlyUpdate";
        }
        if (TextUtils.isEmpty(path)) {
            Log.e("删除日志：","文件路径不正确！");
            return;
        }
        File file = new File(path);
        delFile(file);
    }

    private void delFile(File deleteFile) {
        if (!deleteFile.exists()) {
            return;
        }
        if (!deleteFile.isDirectory()) {
            deleteFile.delete();
        } else {
            File[] fileList=deleteFile.listFiles();
            if (null == fileList || fileList.length <= 0) {
                deleteFile.delete();
                return;
            }
            for (File file : fileList) {
                delFile(file);
            }
            deleteFile.delete();
        }
    }

    private void showTip(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtils.show(BaseActivity.this,str);
            }
        });
    }

    MyDialogView finalDialog=null;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finalDialog=new MyDialogView(BaseActivity.this, "1", "退出应用", "您确定要退出应用吗？", "确定", "取消", new MyDialogView.MyDialogListener() {
                @Override
                public void onClick(int index) {
                    if (index == 0) {
                        finalDialog.dismiss();
                        ActivityUtil.getInstance().finishAllActivity();
                    } else {
                        finalDialog.dismiss();
                    }
                }
            });
            finalDialog.show();
        }

        return super.onKeyDown(keyCode, event);
    }

//    /**
//     * 设置沉浸式状态栏
//     */
//    protected void setStatusBar() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            final ViewGroup linear_bar = (ViewGroup) findViewById(R.id.sou_s);
//            final int statusHeight = getStatusBarHeight();
//            linear_bar.post(new Runnable() {
//                @Override
//                public void run() {
//                    int titleHeight = linear_bar.getHeight();
//                    android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) linear_bar.getLayoutParams();
//                    params.height = statusHeight + titleHeight;
//                    linear_bar.setLayoutParams(params);
//                }
//            });
//        }
//    }
//    /**
//     * 获取状态栏的高度
//     * @return
//     */
//    protected int getStatusBarHeight(){
//        try
//        {
//            Class<?> c=Class.forName("com.android.internal.R$dimen");
//            Object obj=c.newInstance();
//            Field field=c.getField("status_bar_height");
//            int x=Integer.parseInt(field.get(obj).toString());
//            return  getResources().getDimensionPixelSize(x);
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//        return 0;
//    }


}
